package com.epalmpay.job;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.service.prepurchase.IPrePurchaseService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Logger;



/**
 * 预采购的任务
 * 主要讲到期开始的预采购 进行开始，
 * 将新的用户的折扣同步
 * 将 进行中的预采购到期设置结束
 * 将预采购今日收到的钱打给商户
 * 将商户的今日结算金额清空
 * @author Administrator
 *
 */
@Component
public class PrePurseTaskJob {
	
	protected Logger logger = Logger.getLogger(this.getClass());	
	
	@Resource
	private ISysLogService sysLogService;
	@Resource
	private IPrePurchaseService prePurchaseService;
	/**
	 * 每天凌晨4点 执行一次
	 * 对有失败的任务可通过
	 */
	@Scheduled(cron="0 0 4 * * ?")
	//@Scheduled(cron="0 0/1 * * * ?")
	public void  run(){

	String	dateStr = DateUtil.fomatDate(new Date(), DateUtil.sdfDay);
	 try {
		prePurchaseService.finishSycOnStartPrPurs(dateStr);
		StringBuffer sb = new StringBuffer(); 
		logger.info(sb.append("计划任务:")
				.append(TaskType.prePurchStartTask.getName())
				.append(",预采购活动开始:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
		try {
			sysLogService.recordSysTaskLog(TaskType.prePurchStartTask.getType(),
					new Date(), RunStatus.Success.getType(),
					sb.append(",预采购活动开始").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	} catch (ApplicationException e) {
		
		e.printStackTrace();
		try {
			StringBuffer sb = new StringBuffer(); 
			sysLogService.recordSysTaskLog(TaskType.prePurchStartTask.getType(),
					new Date(), RunStatus.Fail.getType(),
					sb.append(",预采购活动开始").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime))
					.append("执行失败！").append(e.getMessage()).toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	 try {
		String DateStr2= DateUtil.fomatDate(DateUtil.getAfterDay(-1),DateUtil.sdfDay);
		prePurchaseService.finishSycOnEndPrPurs(DateStr2);
		StringBuffer sb = new StringBuffer(); 
		logger.info(sb.append("计划任务:")
				.append(TaskType.prePurchEndTask.getName())
				.append(",预采购活动结束:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
		
		try {
			sysLogService.recordSysTaskLog(TaskType.prePurchEndTask.getType(),
					new Date(), RunStatus.Success.getType(),
					sb.append(",预采购活动结束").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	} catch (ApplicationException e) {
		e.printStackTrace();
		try {
			StringBuffer sb = new StringBuffer(); 
			sysLogService.recordSysTaskLog(TaskType.prePurchEndTask.getType(),
					new Date(), RunStatus.Fail.getType(),
					sb.append(",预采购活动结束").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime))
					.append("执行失败！").append(e.getMessage()).toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
    try {
		prePurchaseService.finishSycUserSaleAmount();
		StringBuffer sb = new StringBuffer();
		logger.info(sb.append("计划任务:")
				.append(TaskType.prePurchCustomerBenefitTask.getName())
				.append(",预采购活动同步会员折扣:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
		
		try {
			sysLogService.recordSysTaskLog(TaskType.prePurchCustomerBenefitTask.getType(),
					new Date(), RunStatus.Success.getType(),
					sb.append(",预采购活动同步会员折扣").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	} catch (ApplicationException e) {
		e.printStackTrace();
		try {
			StringBuffer sb = new StringBuffer(); 
			sysLogService.recordSysTaskLog(TaskType.prePurchCustomerBenefitTask.getType(),
					new Date(), RunStatus.Fail.getType(),
					sb.append(",预采购活动同步会员折扣").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime))
					.append("执行失败！").append(e.getMessage()).toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
     
     try {
		prePurchaseService.finishPrePursMerchantSetted(new Date());
		StringBuffer sb = new StringBuffer();
		logger.info(sb.append("计划任务:")
				.append(TaskType.prePurchMerchantSettedTask.getName())
				.append(",商户结算:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
		
		try {
			sysLogService.recordSysTaskLog(TaskType.prePurchMerchantSettedTask.getType(),
					new Date(), RunStatus.Success.getType(),
					sb.append(",商户结算").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	} catch (ApplicationException e) {
		e.printStackTrace();
		try {
			StringBuffer sb = new StringBuffer(); 
			sysLogService.recordSysTaskLog(TaskType.prePurchMerchantSettedTask.getType(),
					new Date(), RunStatus.Fail.getType(),
					sb.append(",商户结算").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime))
					.append("执行失败！").append(e.getMessage()).toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	} 
     
     //预采购统计
     try {
 		prePurchaseService.finishPrePursStaticByShop();
 		StringBuffer sb = new StringBuffer();
 		logger.info(sb.append("计划任务:")
 				.append(TaskType.prePurchMerchantStaticTask.getName())
 				.append(",预采购活动统计:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
 		
 		try {
 			sysLogService.recordSysTaskLog(TaskType.prePurchMerchantStaticTask.getType(),
 					new Date(), RunStatus.Success.getType(),
 					sb.append(",预采购活动统计").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
 		} catch (Exception e1) {
 			e1.printStackTrace();
 		}
 	} catch (ApplicationException e) {
 		e.printStackTrace();
 		try {
 			StringBuffer sb = new StringBuffer(); 
 			sysLogService.recordSysTaskLog(TaskType.prePurchMerchantStaticTask.getType(),
 					new Date(), RunStatus.Fail.getType(),
 					sb.append(",预采购活动统计").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime))
 					.append("执行失败！").append(e.getMessage()).toString());
 		} catch (Exception e1) {
 			e1.printStackTrace();
 		}
 	} 
      
	}
}
